/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	ViewActionLogAdvSys.java
 * 模块说明：	
 * 修改历史：
 * 2007-2-2 - Administrator - 创建。
 */
package com.hd123.rumba.action.log;

import java.util.Date;

import com.hd123.rumba.cartridge.FVRumbaSystemCommon;
import com.hd123.rumba.domain.CDomain;
import com.hd123.rumba.domain.biz.IBDomainMgr;
import com.hd123.rumba.domain.biz.SDomain;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.log.CActionLog;
import com.hd123.rumba.request.ActionAbstractImpl;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.runtime.FormatSettings;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.sql.biz.BResultPage;
import com.hd123.rumba.sql.biz.BSQLSubquery;

/**
 * ActionLog | Action | FVRumbaAdvSys
 * 
 * @author zyb
 * 
 */
public class ViewActionLog extends ActionAbstractImpl {

  protected static final String TEMPLATE_LIST = "log/ActionLogsList.jsp";
  protected static final String TEMPLATE_FILTER = "log/ActionLogsFilter.jsp";

  public static final String CAPTION_VIEW = "操作日志";

  public static final String FUNCV_PREFIX = FVRumbaSystemCommon.FUNCV_PREFIX;
  public static final String PERMISSION_VIEW = FUNCV_PREFIX + ".操作日志.查看权";

  @Override
  public String execute() throws Exception {
    String url = ActionConstants.NOTIMPLEMENTED;
    dd.putFormValue("viewName", "ViewActionLog");

    String startnode = (String) dd.getFormValue("start");
    if (startnode == null)
      startnode = "list";
    if ("list".equals(startnode))
      url = this.executeWithPermission("list", PERMISSION_VIEW);
    else if ("filter".equals(startnode))
      url = this.executeWithPermission("filter", null);

    return url;
  }

  public String list() throws Exception {
    SDomain domain = getFormDomain();
    ActionLogFilter filter = getFormFilter();
    return showList(filter, domain);
  }

  public String filter() throws Exception {
    SDomain domain = getFormDomain();
    ActionLogFilter filter = getFormFilter();
    dd.put("domain", domain);
    dd.put("filter", filter);
    dd.setWorkingTemplate(TEMPLATE_FILTER);
    return ActionConstants.APPLICATIONFRAME;
  }

  public BResultPage queryList(BSQLSubquery bsq, int pageSize, int page) throws Exception {
    try {
      return this.query(bsq, pageSize, page);
    } catch (Exception e) {
      this.reportError("", e);
      throw e;
    }
  }

  private Date parseDateOrTime(String s) throws Exception {
    Date result = null;
    if (s == null || s.length() == 0)
      return result;
    try {
      result = FormatSettings.SDFyMdHms.parse(s);
    } catch (Exception e) {
      try {
        result = FormatSettings.SDFyMd.parse(s);
      } catch (Exception e2) {
      }
    }
    return result;
  }

  private ActionLogFilter getFormFilter() throws Exception {
    ActionLogFilter filter = new ActionLogFilter();
    filter.setTimeAfter(this.parseDateOrTime((String) dd.getFormValue("filter_timeAfter")));
    filter.setTimeBefore(this.parseDateOrTime((String) dd.getFormValue("filter_timeBefore")));
    filter.setOperatorLike((String) dd.validateFormValue("filter_operatorLike", null, false, null,
        CActionLog.CAPTION_OPERATOR));
    filter.setActionCaptionLike((String) dd.validateFormValue("filter_actionCaptionLike", null,
        false, null, CActionLog.CAPTION_ACTIONCAPTION));
    filter.setEventLike((String) dd.validateFormValue("filter_eventLike", null, false, null,
        CActionLog.CAPTION_EVENT));
    filter.setMessageLike((String) dd.validateFormValue("filter_messageLike", null, false, null,
        CActionLog.CAPTION_MESSAGE));
    return filter;
  }

  private String showList(ActionLogFilter filter, SDomain domain) throws Exception {
    dd.put("filter", filter);
    dd.put("domain", domain);
    dd.setWorkingTemplate(TEMPLATE_LIST);
    return ActionConstants.APPLICATIONFRAME;
  }

  private SDomain getFormDomain() throws Exception {
    String domainUuid = (String) dd.getFormValue("domain_uuid");
    if (domainUuid == null)
      domainUuid = dd.getCurrentDomain().getUuid();
    IBDomainMgr dmMgr = (IBDomainMgr) NamingMgr.getInstance().lookupMgr(IBDomainMgr.class);
    SDomain dm = dmMgr.getDomain(domainUuid);
    if (dm == null)
      throw new BusinessException("找不到指定的" + CDomain.CAPTION_CLASS + "(" + domainUuid + ")。");
    return dm;
  }
}
